548B - Mike and Fun - CodeForces Solution


brute force dp greedy implementation *1400

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
 
// -------------------------------------- -------------------------------------- //

//#define int long long
#define mod 1000000007
#define f(a,b) for(int i=a;i<b;i++)
#define fr(a,b) for(int i=a;i>b;i--)
#define endl '\n'
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// order_of_key(), *find_by_order()
vector<int> sieve(int n) {int*arr = new int[n + 1](); vector<int> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}
#define fi for(int i=0;i<n;i++)
#define fj for(int j=0;j<n;j++)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

// -------------------------------------- -------------------------------------- //

int divisors(int n){

    set<int> v;
    for (int i=1; i<=sqrt(n); i++){
        if (n%i == 0)
        {
            if (n/i == i)
                v.insert(i);
  
            else{
                v.insert(i); v.insert(n / i);
            }
        }
    }

    return *(--(--v.end()));

}

int ask_judge(int l, int r){

    if(l >= r) return -1;
    cout<<"? "<<l + 1<<" "<<r + 1<<endl;

    int x; cin>>x;
    
    return x - 1;

}

int isprime(int n){

    for(int i=2;i*i<=n;i++){
        if(n % i == 0) return 0;
    }
    return 1;

}

bool check_palindrome(string a){
    string b = a;
    reverse(a.begin(), a.end());
    return a == b;
}
//vector<bool> vis;

vector<int> parent;
vector<int> rankk;

int findPar(int node){
    if(node == parent[node]){ return node; }
    return parent[node] = findPar(parent[node]);
}

void union_it(int v, int u){
    v = findPar(v); u = findPar(u);

    if(rankk[v] == rankk[u]){
        parent[v] = u;
        rankk[u]++;
    }
    else if(rankk[v] > rankk[u]){
        parent[u] = v;
    }
    else parent[v] = u;

}

vector<int> vis, viss;

int curr;
int cnt;

void dfs(int node, map<int, int>& adj, int start, vector<vector<int>>& tohan){

    vis[node] = 1;
    auto it = adj[node];
    if(!vis[it]){
        tohan[start].push_back(it);
        dfs(it, adj, start, tohan);
    }
    else{
        bool f = 0;
        for(auto i: tohan[start]){
            if(i == it){
                f = 1;
                break;
            }
        }
        if(!f) tohan[start].push_back(it);
        return;
    }
    
}

int helper(int i, int sum, int v2, int t, int d, vector<vector<int>>& dp){

    if(i == t){
        if(sum == v2) return 0;
        return INT_MIN;
    }

    if(dp[i][sum] != -1) return dp[i][sum];

    int op = INT_MIN;
    for(int j=(-d);j<=d;j++){
        int x = helper(i + 1, sum + j, v2, t, d, dp);
        if(x != INT_MIN) op = max(op, (sum + j) + x);
    }

    return dp[i][sum] = op;

}

int32_t main()
{

    IOS;

    int test = 1; //cin>>test;

    while(test--){
    
        //int n; cin>>n;
        //int k; cin>>k;
        //vector<int> v(n); fi cin>>v[i];
        
        int n, m; cin>>n>>m;
        int q; cin>>q;

        vector<vector<int>> v(n, vector<int>(m));
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cin>>v[i][j];
            }
        }

        for(int k=0;k<q;k++){

            int ans = 0;
            int x, y; cin>>x>>y;
            v[x - 1][y - 1] = !v[x - 1][y - 1];
            for(int i=0;i<n;i++){
                int cnt = 0;
                for(int j=0;j<m;j++){
                    if(v[i][j] == 1) cnt++;
                    else cnt = 0;                    
                    ans = max(ans, cnt);
                }
                ans = max(ans, cnt);
            }
            cout<<ans<<endl;

        }

    }

    return 0;
                
}


Comments

Submit
0 Comments
More Questions

734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality